package com.lechi.web.runner;

import com.lechi.iot.netty.utils.NettyCacheUtils;
import com.lechi.web.constants.Constants;
import com.lechi.web.framework.manager.AsyncManager;
import com.lechi.web.server.AppServer;
import jakarta.annotation.PreDestroy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;


/**
 * 确保应用退出时能关闭后台线程
 *
 * @author yg
 */
@Slf4j
@Component
public class ShutdownManager {

    @PreDestroy
    public void destroy() {
        shutdownAsyncManager();
    }

    /**
     * 停止异步执行任务
     */
    private void shutdownAsyncManager() {
        try {
            log.info("====关闭后台任务任务线程池====");
            AsyncManager.me().shutdown();
            final AppServer appServer = NettyCacheUtils.get(Constants.ANDROID_TCP, AppServer.class);
            appServer.stop();
//            GuavaCacheUtil.getBiAndroidDeviceSnAndIPMap().forEach((androidSN, dateTime) -> {
//                if (new Date().getTime() - dateTime.getTime() <= 10000L) {
//                    CompletableFuture.runAsync(IotDeviceCommond.commond(androidSN, AppCmdConstants.STOPED));
//                }
//            });
        } catch (Exception e) {
            log.error("====关闭后台任务任务线程池====", e);
        }
    }
}
